﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer.DAL;

namespace BestBuy.UserControls
{
    [ValidationProperty("SelectedSubCategoryID")]
    public partial class SelectProductCategory : System.Web.UI.UserControl
    {
        public int? SelectedSubCategoryID
        {
            get
            {
                ListBox control;
                switch (CurrentLevel)
                {
                    case 1:
                        control = FirstLevelCatListBox;
                        break;
                    case 2:
                        control = SecondLevelCatListBox;
                        break;
                    case 3:
                        control = ThirdLevelCatListBox;
                        break;
                    default:
                        return null;
                }
                int cat;
                if (Int32.TryParse(control.SelectedValue, out cat))
                {
                    return cat;
                }
                else
                {
                    return null;
                }
            }
        }

        public int CurrentLevel
        {
            get
            {
                object valueObj = ViewState["CurrentLevel"];
                if (valueObj == null)
                {
                    return 0;
                }
                int value = 0;
                Int32.TryParse(valueObj.ToString(), out value);
                return value;
            }
            private set
            {
                ViewState["CurrentLevel"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                CurrentLevel = LoadCategoriesToListBox(FirstLevelCatListBox, null) ? 1 : 0;
            }
        }

        protected void FirstLevelCatListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            int parentCatID = 0;
            if (Int32.TryParse(FirstLevelCatListBox.SelectedValue, out parentCatID))
            {
                CurrentLevel = LoadCategoriesToListBox(SecondLevelCatListBox, parentCatID) ? 2 : 1;
            }
        }

        protected void SecondLevelCatListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            int parentCatID = 0;
            if (Int32.TryParse(SecondLevelCatListBox.SelectedValue, out parentCatID))
            {
                CurrentLevel = LoadCategoriesToListBox(ThirdLevelCatListBox, parentCatID) ? 3 : 2;
            }
        }

        private bool LoadCategoriesToListBox(ListBox control, int? parentCatID)
        {
            var cats = ProductCategoryDAL.GetChildrenOfCategory(parentCatID);
            control.DataSource = cats;
            control.DataBind();
            return cats != null && cats.Count > 0;
        }
    }
}